package com.market.service.impl;

import com.market.common.exception.ServiceException;
import com.market.common.vo.ResponseCode;
import com.market.entity.Role;
import com.market.mapper.RoleMapper;
import com.market.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author :  KanShan
 * @version : 1.8
 * @Date: 2021/12/01/20:41
 * @注释 :
 */
@Service


public class RoleServiceImpl implements RoleService {
    @Resource
    RoleMapper roleMapper;

    @Override
    public int insertRole(Role role) {
        int count = 0;
        List<Role> roles = roleMapper.selectList();
        for (Role role1 : roles) {
            if (role1.getRoleName() != role.getRoleName()) {
                int insert = roleMapper.insert(role);
                if (insert > 0) {
                    count = insert;
                    throw new ServiceException(ResponseCode.SUCCESS);
                } else {
                    throw new ServiceException(ResponseCode.ERROR);
                }
            } else {
                throw new ServiceException(ResponseCode.ERROR);
            }
        }
        return count;
    }

    @Override
    public int updateRole(Role role) {
        int update = roleMapper.update(role);
        if (update > 0) {
            return update;
        } else {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

    @Override
    public int deleteRole(Long id) {
        int delete = roleMapper.delete(id);
        if (delete > 0){
            return delete;
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }

    }

    @Override
    public List<Role> findRole() {
        List<Role> roles = roleMapper.selectList();
        return roles;
    }

    @Override
    public List<Role> findRoleByIdOrName(String string) {
        List<Role> roles = roleMapper.selectListByIdOrName(string);
        return roles;
    }
}
